package com.hnas.ecard.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.ecard.dao.ECardChannelInfoDao;
import com.hnas.ecard.mapper.ECardChannelInfoMapper;
import com.hnas.ecard.orm.ECardChannelInfo;
import com.hnas.ecard.service.dto.DTOECardChannelInfo;
import com.hnas.ecard.service.search.DTOECardChannelInfoSearch;

@Component("eCardChannelInfoDao")
public class ECardChannelInfoDaoImpl extends BaseDaoImpl<ECardChannelInfo> implements ECardChannelInfoDao{
	
	private Logger logger = LoggerFactory.getLogger(ECardChannelInfoDaoImpl.class);

	public ECardChannelInfoDaoImpl() {
		super(ECardChannelInfo.class);
	}

	@Override
	public ECardChannelInfo saveOrUpdate(DTOECardChannelInfo dto) {
		ECardChannelInfo vo = null;
		if(dto != null) {
			if(StringUtil.isNotEmpty(dto.getId())) {
				vo = this.find(dto.getId());
			}
			if(vo == null) {
				vo = new ECardChannelInfo();
				BeanUtils.copyProperties(dto, vo);
				vo.setCreateTime(DateUtil.dateTimeToString(new Date()));
				vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
				vo.setIsDeleted(false);
				this.persist(vo);
			}else {
				BeanUtils.copyProperties(dto, vo,new String[]{"id", "createTime"});
				vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
				this.merge(vo);
			}
		}
		return vo;
	}

	@Override
	public DTOPagedList<DTOECardChannelInfo> page(DTOECardChannelInfoSearch criteria) {
		DTOPagedList<DTOECardChannelInfo> pageList = new DTOPagedList<DTOECardChannelInfo>();
		List<DTOECardChannelInfo> list = new ArrayList<DTOECardChannelInfo>();
		PageBean<ECardChannelInfo> page = new PageBean<ECardChannelInfo>();
		page.setPageNo(criteria.getPageNo());
		page.setPageSize(criteria.getPageSize());
		page.setTotalCount(criteria.getTotalCount());
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		int j = 1;
		if(StringUtil.isNotEmpty(criteria.getChannelCode())){
			sb.append(" and t.channelCode like ?"+j++);
			paras.add("%"+criteria.getChannelCode().trim()+"%");
		}
		if(StringUtil.isNotEmpty(criteria.getChannelName())){
			sb.append(" and t.channelName like ?"+j++);
			paras.add("%"+criteria.getChannelName().trim()+"%");
		}
		if(StringUtil.isNotEmpty(criteria.getRemark())){
			sb.append(" and t.getRemark like ?"+j++);
			paras.add("%"+criteria.getRemark().trim()+"%");
		}
			
		if(StringUtil.isNotEmpty(criteria.getStartDate())){
			sb.append(" and t.createTime >= ?"+j++);
			paras.add(criteria.getStartDate().trim()+" 00:00:00");
		}
		
		if(StringUtil.isNotEmpty(criteria.getEndDate())){
			sb.append(" and t.createTime <= ?"+j++);
			paras.add(criteria.getEndDate().trim()+" 23:59:59");
		}
		
		if(sb.length() > 0){
			sb.delete(0, 5);
			sb.insert(0, " from ECardChannelInfo t  where ");
		}else{
			sb.append(" from ECardChannelInfo t ");
		}
		sb.append(" order by t.createTime desc");
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) "+sb);
		this.queryByPage(page, paras.toArray());
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		for(ECardChannelInfo vo : page.getList()){
			DTOECardChannelInfo dto = new DTOECardChannelInfo();
			dto = ECardChannelInfoMapper.getInstance().mapToValue(vo);
			list.add(dto);
		}
		pageList.setList(list);
		return pageList;
	}

	@Override
	public DTOECardChannelInfo findById(String id) {
		ECardChannelInfo  vo= this.find(id);
		return ECardChannelInfoMapper.getInstance().mapToValue(vo);
	}

	@Override
	public int batchInsert(List<ECardChannelInfo> list) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("insert into tbl_ecard_channel_info(id,channel_code,channel_name,remark,create_time,update_time,is_deleted) values ");
		if(list == null || list.size() <= 0) return 0;
		for(int i = 0; i < list.size(); i++) {
			ECardChannelInfo info = list.get(i);
			sb.append("('").append(info.getId()).append("','")
			.append(info.getChannelCode()).append("','")
			.append(info.getChannelName()).append("','")
			.append(info.getRemark()).append("','")
			.append(info.getCreateTime()).append("','")
			.append(info.getUpdateTime()).append("',")
			.append(info.getIsDeleted()?"1":"0").append("),");
		}
		String sql = sb.substring(0, sb.length() -1);
		logger.info("批量插入sql："+sql);
		Query query = this.entityManager.createNativeQuery(sql);
		return query.executeUpdate();
	}

}
